package com.abvnet.project.water.api.web;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectEqp;
import com.abvnet.project.water.project.service.IProjectEqpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/project/eqp")
@Slf4j
public class ApiProjectEqpController extends BaseController {

    @Autowired
    private IProjectEqpService projectEqpService;

    @GetMapping("/get")
    public AjaxResult get(@RequestParam("projectId") Long projectId) {
        return AjaxResult.success(projectEqpService.selectProjectEqpByProjectId(projectId));
    }

    @PostMapping("/edit")
    public AjaxResult editSave(ProjectEqp projectEqp, @RequestParam(name="aerationEqpFile",required = false) MultipartFile aerationEqpFile,
                               @RequestParam(name="diveEqpFile",required = false) MultipartFile diveEqpFile,
                               @RequestParam(name="sewagePumpFile",required = false) MultipartFile sewagePumpFile,
                               @RequestParam(name="sludgePumpFile",required = false) MultipartFile sludgePumpFile,
                               @RequestParam(name="airFloatFile",required = false) MultipartFile airFloatFile,
                               @RequestParam(name="mbrMemFile",required = false) MultipartFile mbrMemFile,
                               @RequestParam(name="otherEqpFile",required = false) MultipartFile otherEqpFile) {
        try {
            multiFile(projectEqp, aerationEqpFile, diveEqpFile, sewagePumpFile, sludgePumpFile, airFloatFile, mbrMemFile, otherEqpFile);
            return toAjax(projectEqpService.updateProjectEqp(projectEqp));
        } catch (Exception e) {
            log.error("更新设备运行情况失败！", e);
            return error(e.getMessage());
        }
    }

    private void multiFile(ProjectEqp projectEqp, MultipartFile aerationEqpFile,
                           MultipartFile diveEqpFile,
                           MultipartFile sewagePumpFile,
                           MultipartFile sludgePumpFile,
                           MultipartFile airFloatFile,
                           MultipartFile mbrMemFile,
                           MultipartFile otherEqpFile) throws IOException {
        if(StringUtils.isNotEmpty(projectEqp.getAerationEqpDelete())) {
            if(StringUtils.equals("1",projectEqp.getAerationEqpDelete())) {
                projectEqp.setAerationEqpUrl(StringUtils.EMPTY);
            }
        }
        if (aerationEqpFile != null && !aerationEqpFile.isEmpty()) {
            String aerationUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), aerationEqpFile);
            projectEqp.setAerationEqpUrl(aerationUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getDiveEqpDelete())) {
            if(StringUtils.equals("1",projectEqp.getDiveEqpDelete())) {
                projectEqp.setDiveEqpUrl(StringUtils.EMPTY);
            }
        }
        if (diveEqpFile != null && !diveEqpFile.isEmpty()) {
            String diveUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), diveEqpFile);
            projectEqp.setDiveEqpUrl(diveUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getSewagePumpDelete())) {
            if(StringUtils.equals("1",projectEqp.getSewagePumpDelete())) {
                projectEqp.setSewagePumpUrl(StringUtils.EMPTY);
            }
        }
        if (sewagePumpFile != null && !sewagePumpFile.isEmpty()) {
            String sewagePumpUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), sewagePumpFile);
            projectEqp.setSewagePumpUrl(sewagePumpUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getSludgePumpDelete())) {
            if(StringUtils.equals("1",projectEqp.getSludgePumpDelete())) {
                projectEqp.setSludgePumpUrl(StringUtils.EMPTY);
            }
        }
        if (sludgePumpFile != null && !sludgePumpFile.isEmpty()) {
            String sludgePumpUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), sludgePumpFile);
            projectEqp.setSludgePumpUrl(sludgePumpUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getAirFloatDelete())) {
            if(StringUtils.equals("1",projectEqp.getAirFloatDelete())) {
                projectEqp.setAirFloatUrl(StringUtils.EMPTY);
            }
        }
        if (airFloatFile != null && !airFloatFile.isEmpty()) {
            String airFloatUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), airFloatFile);
            projectEqp.setAirFloatUrl(airFloatUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getMbrMemDelete())) {
            if(StringUtils.equals("1",projectEqp.getMbrMemDelete())) {
                projectEqp.setMbrMemUrl(StringUtils.EMPTY);
            }
        }
        if (mbrMemFile != null && !mbrMemFile.isEmpty()) {
            String mbrMemUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), mbrMemFile);
            projectEqp.setMbrMemUrl(mbrMemUrl);
        }
        if(StringUtils.isNotEmpty(projectEqp.getOtherEqpDelete())) {
            if(StringUtils.equals("1",projectEqp.getOtherEqpDelete())) {
                projectEqp.setOtherEqpUrl(StringUtils.EMPTY);
            }
        }
        if (otherEqpFile != null && !otherEqpFile.isEmpty()) {
            String otherUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), otherEqpFile);
            projectEqp.setOtherEqpUrl(otherUrl);
        }
    }
}
